#include "devicecodewidget.h"

#include "highlight-effect.h"

const int BUTTON_WIDTH = 56;
const int BUTTON_HEIGHT = 56;

DeviceCodeWidget::DeviceCodeWidget(QWidget *parent) : QWidget(parent)
{
    initUI();
}

void DeviceCodeWidget::initUI()
{
    QVBoxLayout *mainVLayout = new QVBoxLayout();
    mainVLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(mainVLayout);

    QHBoxLayout *connectCodeHLayout = new QHBoxLayout();
    QHBoxLayout *describeHLayout = new QHBoxLayout();
    QHBoxLayout *lineEditHLayout = new QHBoxLayout();
    QHBoxLayout *startConnectHLayout = new QHBoxLayout();

    QFont font;
    font.setBold(true);
    QLabel *connectCodeLab = new QLabel(this);
    connectCodeLab->setText(
        tr("Please enter the connection code of the other device")); // Please enter the other party's connection code
    connectCodeLab->setFont(font);

    connectCodeHLayout->addStretch();
    connectCodeHLayout->addWidget(connectCodeLab);
    connectCodeHLayout->addStretch();
    connectCodeHLayout->setSpacing(0);

    QLabel *describeLab = new QLabel(this);
    describeLab->setText(tr("The connection code of the device can be obtained on the homepage of the other party's "
                            "'multi terminal collaboration' application"));
    QPalette palette = describeLab->palette();
    palette.setColor(QPalette::Text, QColor("#67676C"));
    describeLab->setPalette(palette);

    describeHLayout->addStretch();
    describeHLayout->addWidget(describeLab);
    describeHLayout->addStretch();

    m_firstBtn = new QPushButton(this);
    m_firstBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_secondBtn = new QPushButton(this);
    m_secondBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_thirdBtn = new QPushButton(this);
    m_thirdBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_fourthBtn = new QPushButton(this);
    m_fourthBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_fifthBtn = new QPushButton(this);
    m_fifthBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_sixthBtn = new QPushButton(this);
    m_sixthBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_seventhBtn = new QPushButton(this);
    m_seventhBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_eighthBtn = new QPushButton(this);
    m_eighthBtn->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);

    lineEditHLayout->addStretch();
    lineEditHLayout->addWidget(m_firstBtn);
    lineEditHLayout->addWidget(m_secondBtn);
    lineEditHLayout->addWidget(m_thirdBtn);
    lineEditHLayout->addWidget(m_fourthBtn);
    lineEditHLayout->addWidget(m_fifthBtn);
    lineEditHLayout->addWidget(m_sixthBtn);
    lineEditHLayout->addWidget(m_seventhBtn);
    lineEditHLayout->addWidget(m_eighthBtn);
    lineEditHLayout->addStretch();

    m_startConnectBtn = new QPushButton(this);
    m_startConnectBtn->setFixedSize(160, 48);
    m_startConnectBtn->setText(tr("Connection")); // Start Connection
    m_startConnectBtn->setFocusPolicy(Qt::NoFocus);
    m_startConnectBtn->setProperty("isImportant", true);
    m_startConnectBtn->setEnabled(false);

    startConnectHLayout->addStretch();
    startConnectHLayout->addWidget(m_startConnectBtn);
    startConnectHLayout->addStretch();

    mainVLayout->addStretch();
    mainVLayout->addLayout(connectCodeHLayout);
    mainVLayout->addSpacing(16);
    mainVLayout->addLayout(describeHLayout);
    mainVLayout->addSpacing(40);
    mainVLayout->addLayout(lineEditHLayout);
    mainVLayout->addSpacing(61);
    mainVLayout->addLayout(startConnectHLayout);
    mainVLayout->addStretch();

    connect(m_startConnectBtn, &QPushButton::clicked, this, &DeviceCodeWidget::slotStartConnectBtnClicked);
}

void DeviceCodeWidget::slotStartConnectBtnClicked()
{
    QString lineEditText = getCodeText();
    qInfo() << "lineEditText" << lineEditText;
    Q_EMIT sigLineEditText(lineEditText);
}

QString DeviceCodeWidget::getCodeText()
{
    QString codeText = m_firstBtn->text() + m_secondBtn->text() + m_thirdBtn->text() + m_fourthBtn->text()
                       + m_fifthBtn->text() + m_sixthBtn->text() + m_seventhBtn->text() + m_eighthBtn->text();
    return codeText;
}

void DeviceCodeWidget::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key::Key_Escape) {
        return;
    } else if (event->key() == Qt::Key::Key_Shift) {
        return;
    }

    if ((event->key() == Qt::Key_Backspace)) {
        switch (m_index) {
        case 1:
            m_firstBtn->setText(QString(""));
            break;
        case 2:
            m_secondBtn->setText(QString(""));
            break;
        case 3:
            m_thirdBtn->setText(QString(""));
            break;
        case 4:
            m_fourthBtn->setText(QString(""));
            break;
        case 5:
            m_fifthBtn->setText(QString(""));
            break;
        case 6:
            m_sixthBtn->setText(QString(""));
            break;
        case 7:
            m_seventhBtn->setText(QString(""));
            break;
        case 8:
            m_eighthBtn->setText(QString(""));
            break;
        default:
            break;
        }
        if (m_index > 0) {
            m_index--;
        } else {
            return;
        }
    } else {
        if (m_index < 8) {
            m_index++;
        } else {
            return;
        }
        switch (m_index) {
        case 1:
            m_firstBtn->setText(QString(event->key()).toUpper());
            break;
        case 2:
            m_secondBtn->setText(QString(event->key()).toUpper());
            break;
        case 3:
            m_thirdBtn->setText(QString(event->key()).toUpper());
            break;
        case 4:
            m_fourthBtn->setText(QString(event->key()).toUpper());
            break;
        case 5:
            m_fifthBtn->setText(QString(event->key()).toUpper());
            break;
        case 6:
            m_sixthBtn->setText(QString(event->key()).toUpper());
            break;
        case 7:
            m_seventhBtn->setText(QString(event->key()).toUpper());
            break;
        case 8:
            m_eighthBtn->setText(QString(event->key()).toUpper());
            break;
        default:
            break;
        }
    }
    QString text = getCodeText();
    if (text.size() == 8) {
        m_startConnectBtn->setEnabled(true);
    } else {
        m_startConnectBtn->setEnabled(false);
    }
    QWidget::keyPressEvent(event);
}
